Sequlize & Express. P3


Posted by Rich on 2021-08-13

如何透過 Sequlize 操作資料庫?

  1. Simple INSERT queries
    官方範例:
    const jane = await User.create({ firstName: "Jane", lastName: "Doe" });
    意思:在 User 資料庫裡新增一筆資料。firstName 叫 Jane,lastName 叫 Doe。回傳的結果存在 jane
    記得 await 只能放在 async function 裡面。

  2. Simple SELECT queries
    官方範例:const users = await User.findAll();
    意思:撈出 User 裡面全部的資料。回傳的結果存在 users
    如果只想找一筆資料,像是想要比對使用者的帳號密碼輸入是否正確。

     const user = await User.findOne({
         where: {
         username, //ES6方便寫法 
         password
       }
     })
    

    意思:在User找到一筆 username = username, password = password的資料。

  3. Simple DELETE queries

         await User.destroy({
       where: {
         firstName: "Jane"
       }
     });
    

    刪除 firstName: Jane 的這筆資料。(不確定會不會相符的全刪?)

  4. Simple UPDATE queries

     await User.update({ lastName: "Doe" }, {
       where: {
         lastName: null
       }
     });
    

    更新 lastName: null 的資料,更新為 lastName: "Doe"。不確定會不會相符的全部更新?)

官方文件


#Sequelize







Related Posts

Redux basic

Redux basic

What Type of Laser Engraving Machine Should be Used for Stainless Steel Engraving?

What Type of Laser Engraving Machine Should be Used for Stainless Steel Engraving?

CSS keylogger:攻擊與防禦

CSS keylogger:攻擊與防禦


Comments